﻿using PlatformNetCore.Common;
using PlatformNetCore.Common.Helper;
using PlatformNetCore.Entity;
using PlatformNetCore.IService;

namespace PlatformNetCore.Service
{
    public class OAuthService : IOAuthService
    {
        /// <summary>
        /// 获取 AccessToken
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public OAuthResponse GetAccessToken(string url, OAuthRequest request)
        {
            string responseData = HttpHelper.Post("GetAccessToken", string.Empty, string.Format("{0}/connect/token", url), CommonHelper.GetJoinStr(request), CommonConst.ContentTypeXwwwFormUrlencoded, 1);
            return JsonHelper.DeserializeObject<OAuthResponse>(responseData);
        }

        public OAuthResponse GetAccessTokenFullUrl(string url, OAuthRequest request)
        {
            string responseData = HttpHelper.Post("GetAccessToken", string.Empty, url, CommonHelper.GetJoinStr(request), CommonConst.ContentTypeXwwwFormUrlencoded, 1);
            return JsonHelper.DeserializeObject<OAuthResponse>(responseData);
        }

        /// <summary>
        /// RefreshToken
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public OAuthResponse RefreshToken(string url, OAuthRefreshRequest request)
        {
            string responseData = HttpHelper.Post("RefreshToken", string.Empty, string.Format("{0}/connect/token", url), CommonHelper.GetJoinStr(request), CommonConst.ContentTypeXwwwFormUrlencoded, 1);
            return JsonHelper.DeserializeObject<OAuthResponse>(responseData);
        }
    }
}
